﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Blazing.BusinessObjects.Widgets;
using System.BusinessObjects.With;
using Blazing.BusinessObjects.Cms;
using Iesi.Collections.Generic;
using NHibernate.Criterion;
using System.BusinessObjects.Transactions;

namespace Blazing.WebFacade
{
    [System.ComponentModel.DataObject]
    public class WidgetController
    {
        #region Widgets
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void DeleteWidget(Widget obj)
        {
            Widget objWidget = Widget.Load(obj.ID);
            objWidget.Delete();
            With.Transaction.Execute(() => objWidget.Save());
            objWidget.Evict();
            Site.Evict(objWidget.Site.ID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public Widget NewWidget(Widget widget)
        {
            With.Transaction.Execute(() => widget.Save());
            Site.Evict(widget.Site.ID);
            return widget;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public Widget UpdateWidget(Widget widget)
        {
            With.Transaction.Execute(() => widget.Save());
            widget.Evict();
            Site.Evict(widget.Site.ID);
            return widget;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public ISet<Widget> GetWidgets(int siteId)
        {
            Site obj = Site.Load(siteId);
            return obj != null ? obj.SiteWidgets : null;
        }
        #endregion

        #region WidgetRegion
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void DeleteWidgetRegion(WidgetRegion obj)
        {
            WidgetRegion objWidget = WidgetRegion.Load(obj.ID);
            objWidget.Delete();
            With.Transaction.Execute(() => objWidget.Save());
            objWidget.Evict();
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public WidgetRegion NewWidgetRegion(WidgetRegion widget)
        {
            With.Transaction.Execute(() => widget.Save());
            return widget;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public WidgetRegion UpdateWidgetRegion(WidgetRegion widget)
        {
            With.Transaction.Execute(() => widget.Save());
            return widget;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public IList<WidgetRegion> GetWidgetRegions()
        {
            IList<WidgetRegion> safeList = WidgetRegion.Search(Order.Asc("Name"));
            safeList = System.BusinessObjects.Data.SafeDataBindingList<WidgetRegion>.Create(WidgetRegion.Search(Order.Asc("Name")));
            return safeList;
        }
        #endregion
    }
}
